﻿Imports OMANE.Common.UI
Imports System.Windows.Input
Imports PTIC_GestionBDD
Imports Microsoft.Practices.Prism.Commands

Namespace ViewModels
    Public Class ViewModelCmdEncoursStats
        Inherits ViewModelCommand
        Implements IViewModelTabSection

        Private _model As ModelTauxGarantis
        Public Property IsClosable As Boolean Implements IViewModelTabSection.IsClosable

        Public Sub New(ByVal Model As ModelTauxGarantis, ByVal DisplayName As String, ByVal Command As ICommand)
            MyBase.New(DisplayName, Command)
            _model = Model
            IsClosable = False
        End Sub
        Public Sub New()
            Title = "STATISTIQUES"
            IsClosable = False
        End Sub

        Private _TablesStatistiques As DataTable
        Public ReadOnly Property TablesStatistiques As DataTable
            Get
                If _TablesStatistiques Is Nothing Then
                    Dim traitStats As RequeteBDD
                    _model = Locator.ModelStatic
                    traitStats = _model.ListeTraitements.TraitTbleEncoursStats
                    traitStats.Calculer_DataTable()
                    _TablesStatistiques = traitStats.DataTableResultat
                End If
                Return _TablesStatistiques
            End Get
        End Property

        Public Property Icone As String Implements Common.UI.IViewModelTabSection.Icone
        Public Property Title As String Implements Common.UI.IViewModelTabSection.Title

        Private _DureeSouscriptionPonderee As IEnumerable
        Public ReadOnly Property DureeSouscriptionPonderee As IEnumerable
            Get
                If Not TablesStatistiques Is Nothing Then
                    If _DureeSouscriptionPonderee Is Nothing Then
                        Dim q = (From e In TablesStatistiques
                                 Group By Label = CStr(e("id_type_pret"))
                                 Into ValueSum = Sum(CDbl(e("CSR")) * CDbl(e("Duree_Actu"))),
                                      ValueWeight = Sum(CDbl(e("CSR")))).ToArray()

                        Dim qresult = From e In q
                                      Select Label = e.Label,
                                             Value = e.ValueSum / e.ValueWeight

                        _DureeSouscriptionPonderee = qresult.ToList()
                    End If

                    Return _DureeSouscriptionPonderee
                Else
                    Return Nothing
                End If
            End Get
        End Property

        Private _TauxNominauxPonderee As IEnumerable
        Public ReadOnly Property TauxNominauxPonderee As IEnumerable
            Get
                If Not TablesStatistiques Is Nothing Then
                    If _TauxNominauxPonderee Is Nothing Then
                        Dim q = (From e In TablesStatistiques
                                 Group By Label = CStr(e("id_type_pret"))
                                 Into ValueSum = Sum(CDbl(e("CSR")) * CDbl(e("TxNom_Actu"))),
                                      ValueWeight = Sum(CDbl(e("CSR"))))



                        Dim qresult = From e In q
                                      Select Label = e.Label,
                                             Value = e.ValueSum / e.ValueWeight

                        _TauxNominauxPonderee = qresult.ToList()
                    End If

                    Return _TauxNominauxPonderee
                Else
                    Return Nothing
                End If
            End Get
        End Property

        Private _CsrParAge As IEnumerable
        Public ReadOnly Property CsrParAge As IEnumerable
            Get
                If Not TablesStatistiques Is Nothing Then
                    If _CsrParAge Is Nothing Then
                        Dim q = (From e In TablesStatistiques
                                Group By Label = CStr(e("tr_age"))
                                Into Value = Sum(CDbl(e("CSR")))).ToArray()

                        Dim qtot = Aggregate e In q Into Sum(e.Value)


                        Dim qresult = From e In q
                                      Select Label = e.Label,
                                             Value = e.Value / qtot

                        _CsrParAge = qresult.ToList()
                    End If

                    Return _CsrParAge
                Else
                    Return Nothing
                End If
            End Get
        End Property

        Private _CsrParPret As IEnumerable
        Public ReadOnly Property CsrParPret As IEnumerable
            Get
                If Not TablesStatistiques Is Nothing Then
                    If _CsrParPret Is Nothing Then
                        Dim q = (From e In TablesStatistiques
                                 Group By Label = CStr(e("id_type_pret"))
                                 Into Value = Sum(CDbl(e("CSR")))).ToArray()

                        Dim qtot = Aggregate e In q Into Sum(e.Value)


                        Dim qresult = From e In q
                                      Select Label = e.Label,
                                             Value = e.Value / qtot

                        _CsrParPret = qresult.ToList()
                    End If

                    Return _CsrParPret
                Else
                    Return Nothing
                End If
            End Get
        End Property

        Public ReadOnly Property UpdateCommand As DelegateCommand(Of Object)
            Get
                Return New DelegateCommand(Of Object)(AddressOf Update)
            End Get
        End Property

        Private Sub Update(param As Object)
            Me._TablesStatistiques = Nothing
            Me._DureeSouscriptionPonderee = Nothing
            Me._TauxNominauxPonderee = Nothing
            Me._CsrParAge = Nothing
            Me._CsrParPret = Nothing

            Me.OnPropertyChanged("DureeSouscriptionPonderee")
            Me.OnPropertyChanged("TauxNominauxPonderee")
            Me.OnPropertyChanged("CsrParAge")
            Me.OnPropertyChanged("CsrParPret")
        End Sub
    End Class
End Namespace
